//
//  ShadowView.swift
//  FreeShadow
//
//  Created by hongyan.yang on 2021/9/10.
//

import UIKit

extension UIView {
    
    func draw(shadow: NSShadow, path: CGPath)->CAShapeLayer{
        let layer = CAShapeLayer()
        layer.shadowColor = (shadow.shadowColor as! UIColor).cgColor
        layer.shadowOpacity = 1
        layer.shadowOffset = shadow.shadowOffset
        layer.shadowRadius = shadow.shadowBlurRadius
        layer.shadowPath = path
        self.layer.addSublayer(layer)
        return layer
    }
}
protocol ShadowView: UIView {
    var shadow: NSShadow{ get set }
    var shadowPath: UIBezierPath { get set }
    var intrinsicSize: CGSize { get set }
}

extension ShadowView {
    func drawShadow(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()!
        context.saveGState()
        let color = shadow.shadowColor as! UIColor
        context.setShadow(offset: shadow.shadowOffset, blur: shadow.shadowBlurRadius, color: color.cgColor)
        color.setFill()
        shadowPath.fill()
        context.restoreGState()
    }
}
